<?php 
	require_once "include_phpuploader.php" ;
	define('TPL_MODULE','./modules/');
	define('CLASS_DIR','class/');
	define('TPL_DIR_ADMIN','templates/admin/');
	define('SRC_DIR_ADMIN','sources/admin/');
	define('CLASS_DIR_ADMIN','class/');
	
	
	include("../../../includes/config.php");
	include("../../../includes/constant.php");
	
	include("../../../includes/functions.php");
	include("../../../includes/va_db.php");
	include("../../../class/categories.class.php");
?>

<?php

set_time_limit(3600);

$uploader=new PhpUploader();

$uploader->PreProcessRequest();



$mvcfile=$uploader->GetValidatingFile();

if($mvcfile->FileName=="thisisanotvalidfile")
{
	$uploader->WriteValidationError("My custom error : Invalid file name. ");
	exit(200);
}


if( $uploader->SaveDirectory )
{
	if(!$uploader->AllowedFileExtensions)
	{
		$uploader->WriteValidationError("When using SaveDirectory property, you must specify AllowedFileExtensions for security purpose.");
		exit(200);
	}

	$cwd=getcwd();
	chdir( dirname($uploader->_SourceFileName) );
	if( ! is_dir($uploader->SaveDirectory) )
	{
		$uploader->WriteValidationError($uploader->SaveDirectory." Invalid SaveDirectory ! not exists.");
		exit(200);
	}
	chdir( $uploader->SaveDirectory );
	$wd=getcwd();
	chdir($cwd);

	$targetfilepath=  "$wd/" . $mvcfile->FileName;
	
	$file_name = $mvcfile->FileName;
	
	if( file_exists ($targetfilepath) )
	{
		//unlink($targetfilepath);
		$nowtime = time();			
		
		$file_name = substr($file_name,0, strlen($file_name)-4).'-'.$nowtime.substr($file_name,strlen($file_name)-4,4);
		
		$targetfilepath = substr($targetfilepath,0, strlen($targetfilepath)-4).'-'.$nowtime.substr($targetfilepath,strlen($targetfilepath)-4,4);
	}
	$arr['url'] = 'kam/upload/images/multiFile/'.$file_name;
	$mvcfile->CopyTo( $targetfilepath );
}

$uploader->WriteValidationOK();

global $db;
$table_edit = 'image_post_silder';


if($_SESSION['type_id'] == '1')
{
	$article_id = (isset($_SESSION['articles_id'])?$_SESSION['articles_id']:'');
	if($article_id != '')
	{
		$arr['article_id'] = $article_id;
	}
}
else
if($_SESSION['type_id'] == '2')
{

	$product_id = (isset($_SESSION['products_id'])?$_SESSION['products_id']:'');
	if($product_id != '')
	{
		$arr['product_id'] = $product_id;
	}
}
else
if($_SESSION['type_id'] == '3')
{

	$img_id = (isset($_SESSION['img_id'])?$_SESSION['img_id']:'');
	if($img_id != '')
	{
		$arr['img_id'] = $img_id;
	}
}
//echo $table_edit;	
vaInsert($table_edit,$arr);	

//echo $targetfilepath;
//$_SESSION['temp-upload'] = $arr;

?>